<div class="wrapper">
    <div>
        <div class="box box-top">
            <!-- /.box-header -->
            <!-- form start -->
            <form id="teamForm" role="form" action="${base}/admin/team/save" method="post">
                <input type="hidden" name="id" value="${(team.id)!}">
                <div class="box-body">
                    <div class="form-group">
                        <label for="schoolId">学校</label>
                        <!--<input type="text" value="${(department.schoolId)!}" class="form-control" name="schoolId" id="schoolId">-->
                        <select class="form-control select2" style="width: 100%;" id="schoolId"
                                onchange="selectDepartment()">
                            <option>--请选择学校--</option>
                            <#list schoolList as school>
                                <option
                                <#if school.id == (grade.department.schoolId)!0> selected="selected"</#if>
                                value="${school.id}">${school.name}</option>
                            </#list>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="departmentId">学部</label>
                        <!--<input type="text" value="${(department.schoolId)!}" class="form-control" name="schoolId" id="schoolId">-->
                        <select class="form-control select2" style="width: 100%;" id="departmentId" name="departmentId"
                                onchange="selectGrade()">
                            <option>--请选择学部--</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="gradeId">年级</label>
                        <!--<input type="text" value="${(department.schoolId)!}" class="form-control" name="schoolId" id="schoolId">-->
                        <select class="form-control select2" style="width: 100%;" id="gradeId" name="gradeId">
                            <option>--请选择年级--</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="name">Name</label>
                        <input type="text" value="${(team.name)!}" class="form-control" name="name" id="name">
                    </div>

                    <div class="form-group">
                        <label for="code">Code</label>
                        <input type="text" value="${(team.code)!}" class="form-control" name="code" id="code">
                    </div>

                    <div class="form-group">
                        <label for="memo">Note</label>
                        <input type="text" value="${(team.memo)!}" class="form-control" name="memo" id="memo">
                    </div>

                    <!--<div class="form-group">-->
                    <!--<label for="gradeId">年级</label>-->
                    <!--<input type="text" value="${(team.gradeId)!}" class="form-control" name="gradeId" id="gradeId">-->
                    <!--</div>-->

                    <div class="form-group">
                        <label for="valid">Valid</label>
                        <select name="valid" id="valid" class="form-control">
                            <option value="true"
                            <#if ((team.valid)!true)==true> selected="selected"</#if>
                            >----Valid----</option>
                            <option value="false"
                            <#if ((team.valid)!true)==false> selected="selected"</#if>
                            >----Invalid----</option>
                        </select>
                    </div>

                    <div class="form-group">
                        <label for="ordinal">SequenceNo</label>
                        <input type="text" value="${(team.ordinal)!}" class="form-control" name="ordinal" id="ordinal">
                    </div>

                    <div class="box-footer">
                        <button type="button" onclick="$('#teamForm').submit();" class="btn btn-primary">Save
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(function () {
        $("#teamForm").validate({
            submitHandler: function (form) {
                submitTeam();
            }
        });
        $('.select2').select2();
    });
    function submitTeam() {
        HF.ajaxFormSubmit('teamForm', function (result) {
            alert("Save Success");
            document.location.reload();
        }, function (e) {
            alert("Save Failure");
        });
    }
    //选择学校联动学部
    function selectDepartment() {
        // 选中学校
        var shcoolId = $("#schoolId").val();
        $("#departmentId").find("option:not(:first)").remove();
        $.ajax({
            url: "${base}/admin/grade/findDepartment?schoolId=" + shcoolId,
            type: "POST",
            dataType: "json",
            success: function (data) {
                var result = eval(data);
                var options = [];
                for (var i in result) {

                    options.push({id: +result[i].id, text: result[i].name});
                }
                //重绘Select2
                $("#departmentId").select2({
                    data: options
                });
            }
        })
    }
    //选择学部联动年级
    function selectGrade() {
        // 选中学部
        var departmentId = $("#departmentId").val();
        $("#gradeId").find("option:not(:first)").remove();
        $.ajax({
            url: "${base}/admin/team/findGrade?departmentId=" + departmentId,
            type: "POST",
            dataType: "json",
            success: function (data) {
                var result = eval(data);
                var options = [];
                for (var i in result) {

                    options.push({id: +result[i].id, text: result[i].name});
                }
                //重绘Select2
                $("#gradeId").select2({
                    data: options
                });
            }
        })
    }
</script>
